layui.use(['table', 'element', 'form', 'laydate', "code"], function () {
});

$(function (){
    list_table();
});

/**
 * 页面表格渲染
 */
function list_table() {
    $("#table_data").html("");
    $("#jobManagerRunning").html("");
    $("#jobManagerPending").html("");
    $("#jobManagerUnknown").html("");
    $("#taskManagerRunning").html("");
    $("#taskManagerPending").html("");
    $("#taskManagerUnknown").html("");

    $.ajax({
        type: "POST",
        url: "/flink-service-list-data",
        contentType: "application/json",
        data: JSON.stringify({
            "route_id": $("#route_id").val(),
            "job_manager_status": $("#job_manager_status").val(),
            "task_manager_status": $("#task_manager_status").val()
        }),
        success: function (req) {
            $("#table_data").html(req.table);
            $("#jobManagerRunning").html(req.jobManagerRunning);
            $("#jobManagerPending").html(req.jobManagerPending);
            $("#jobManagerUnknown").html(req.jobManagerUnknown);
            $("#taskManagerRunning").html(req.taskManagerRunning);
            $("#taskManagerPending").html(req.taskManagerPending);
            $("#taskManagerUnknown").html(req.taskManagerUnknown);
        }
    });
}

function refresh() {
    list_table();
}

/**
 * 新增项目
 */
function save_project() {
    document.cookie = "edit-project-edit-type=1";
    xadmin.open("新增项目",'/flink-project-edit.html', 800, 820, false);
}

/**
 * 编辑项目
 */
function edit_project(id) {
    $.get("/flink_config/getProjectById?id=" + id, function(req) {
        document.cookie = "edit-project-edit-type=2";
        document.cookie = "edit-project-id=" + req.id;
        document.cookie = "edit-project-gateway-num=" + req.gatewayNum;

        document.cookie = "edit-project-route-id=" + req.routeId;
        document.cookie = "edit-project-route-uri=" + req.routeUri;
        document.cookie = "edit-project-route-predicates=" + req.routePredicates;
        document.cookie = "edit-project-route-filters-stripprefix=" + req.routeFiltersStripprefix;

        document.cookie = "edit-project-project-summary=" + req.projectSummary;
        document.cookie = "edit-project-project-detail=" + req.projectDetail;

        document.cookie = "edit-project-flink-version=" + req.flinkVersion;
        document.cookie = "edit-project-start-class=" + req.startClass;
        document.cookie = "edit-project-task-manager-memory=" + req.taskManagerMemory;
        document.cookie = "edit-project-start-jar-name=" + req.startJarName;
        document.cookie = "edit-project-image-name=" + req.imageName;

        xadmin.open("编辑项目",'/flink-project-edit.html', 800, 820, false);
    });
}


/**
 * 删除项目
 */
function del_project(id, routeId) {
    layer.confirm('确认删除(' + routeId + ')吗？', {btn: ['确定','取消']}, function(){
        $.get("/flink_config/del?id=" + id, function(req) {
            if (req === 'success') {
                layer.msg('操作成功');
                window.setTimeout(function () {
                    // parent.refresh();
                    list_table();
                }, 1000);
            } else {
                layer.msg(req);
            }
        });
    }, function(){
    });
}

/**
 * 路由缓存查看
 */
function route_cache() {
    $.get("/flink_config/route/view", function(req) {
        layer.open({
            area:["800px", "400px"],
            title: 'Flink网关路由缓存',
            content: req
        });
    });
}

/**
 * 打开新窗口
 * @param url
 */
function openNewTab(url) {
    window.open (url)
}

/**
 * 打开flink弹框
 * @param name
 */
function openFlink(name, jobManagerSize) {
    if (jobManagerSize < 1) {
        layer.msg('请先启动服务');
        return;
    }
    document.cookie = "current-flink-name=" + name;
    parent.flinkOpen();
}

/**
 * 打开JobManager弹框
 * @param name
 */
function openJobManager(name) {
    var title = "JobManager：" + name;
    document.cookie = "exec-shell-name=" + name;
    xadmin.open(title,'/flink-list-job.html', null, null, true);
}

/**
 * 打开TaskManager弹框
 * @param name
 */
function openTaskManager(name) {
    var title = "TaskManager：" + name;
    document.cookie = "exec-shell-name=" + name;
    xadmin.open(title,'/flink-list-task.html', null, null, true);
}

/**
 * 启动-无SAVEPOINT
 * @param name
 */
function startNoSavePoint(id, name) {
    var title = "启动-无SAVEPOINT：" + name;
    document.cookie = "exec-shell-id=" + id;
    document.cookie = "exec-shell-name=" + name;
    document.cookie = "exec-shell-type=startNoSavePoint";
    xadmin.open(title,'/flink-operate.html', null, null, true);
}

/**
 * 启动-使用SAVEPOINT
 */
function startWithSavePoint(id, name) {
    var title = "SAVEPOINT历史：" + name;
    document.cookie = "exec-shell-id=" + id;
    document.cookie = "exec-shell-name=" + name;
    document.cookie = "exec-shell-type=startWithSavePoint";
    xadmin.open(title,'/flink-savepoint-history.html', null, null, true);
}

/**
 * 停止-SAVEPOINT
 */
function stopWithSavePoint(id, name) {
    var title = "停止-SAVEPOINT：" + name;
    document.cookie = "exec-shell-id=" + id;
    document.cookie = "exec-shell-name=" + name;
    document.cookie = "exec-shell-type=stopWithSavePoint";
    xadmin.open(title,'/flink-operate.html', null, null, true);
}

/**
 * 操作历史
 */
function flinkHistory(id, name) {
    var title = "操作历史：" + name;
    document.cookie = "flink-project-history-id=" + id;
    xadmin.open(title,'/flink-project-history.html', null, null, true);
}

/**
 * 监控重启
 */
function monitorRestart(id, num, name) {
    var title = "监控重启配置：" + name;
    document.cookie = "flink-project-monitor-restart-id=" + id;
    document.cookie = "flink-project-monitor-restart-num=" + num;
    xadmin.open(title,'/flink-monitor-restart.html', 600, 270, false);
}